package com.zr.servlet;

import com.alibaba.fastjson.JSON;
import com.zr.service.UserService;
import com.zr.service.impl.UserServiceImpl;
import com.zr.utils.ResultObj;
import com.zr.vo.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/list")
public class UserListServlet extends HttpServlet {
	//这是一个注释
	//哈哈哈哈啊哈哈哈哈
    //嘿嘿嘿嘿嘿
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String value = req.getReader().readLine();
        User user = JSON.parseObject(value, User.class);
        //查询当前搜索条件下的所有数据条数
        int total = userService.count(user);
        //查询当前搜索条件下，某一页的数据集合
        List<User> userList = userService.list(user);
        PrintWriter out = resp.getWriter();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("total", total);
        paramMap.put("list", userList);
        out.write(JSON.toJSONString(ResultObj.success(200, "查询成功", paramMap)));
        out.flush();
    }
}
